package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import androidx.compose.material3.Icon
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.R
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.RoundCheckbox
import com.promise.jokerdream.ui.components.TimePickerDialog
import com.promise.jokerdream.ui.components.SelectionRow
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.ui.theme.AppFontSizes
import com.promise.jokerdream.database.entity.AscendHeavenGameConfigEntity
import com.promise.jokerdream.config.game.AscendHeavenGameMode
import com.promise.jokerdream.config.game.WulinRegistrationType
import com.promise.jokerdream.ui.components.CheckboxOptionRow

/**
 * 飞升大作战设置卡片
 */
@Composable
fun AscendHeavenSettingCard(
    config: AscendHeavenGameConfigEntity,
    onAscensionBattleChange: (Boolean) -> Unit,
    onGameModeChange: (AscendHeavenGameMode) -> Unit,
    onScheduledChallengeChange: (Boolean) -> Unit,
    onRegistrationTimeChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    SettingCard(
        title = "飞升大作战",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onAscensionBattleChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(vertical = AppSpacing.componentSmall),
                        horizontalArrangement = Arrangement.spacedBy(AppSpacing.componentMedium)
                    ) {
                        // 排位模式
                        CheckboxOptionRow(
                            title = AscendHeavenGameMode.RANKED.displayName,
                            checked = config.gameMode == "RANKED",
                            onCheckedChange = { onGameModeChange(AscendHeavenGameMode.RANKED); },
                            modifier = Modifier.weight(1f)
                        )
                        
                        // 匹配模式
                        CheckboxOptionRow(
                            title = AscendHeavenGameMode.MATCH.displayName,
                            checked = config.gameMode == "MATCH",
                            onCheckedChange = { onGameModeChange(AscendHeavenGameMode.MATCH) },
                            modifier = Modifier.weight(1f)
                        )
                        
                        // 双排模式
                        CheckboxOptionRow(
                            title = AscendHeavenGameMode.DUO.displayName,
                            checked = config.gameMode == "DUO",
                            onCheckedChange = { onGameModeChange(AscendHeavenGameMode.DUO) },
                            modifier = Modifier.weight(1f)
                        )
                    }
                    
                    // 定时挑战选项
                    CheckboxOptionRow(
                        title = "定时挑战",
                        checked = config.scheduledChallengeEnabled,
                        onCheckedChange = onScheduledChallengeChange
                    )
                    
                    // 只有选中定时挑战时才显示时间选择
                    if (config.scheduledChallengeEnabled) {
                        RegistrationTimeSelector(
                            selectedTime = config.registrationTime,
                            onTimeChange = onRegistrationTimeChange
                        )
                    }
                }
            }
        }
    )
}


/**
 * 报名时间选择器
 */
@Composable
private fun RegistrationTimeSelector(
    selectedTime: String,
    onTimeChange: (String) -> Unit
) {
    var showTimePicker by remember { mutableStateOf(false) }
    
    SelectionRow(
        label = "报名时间：",
        selectedText = selectedTime,
        onClick = { showTimePicker = true }
    )
    
    // 时间选择器对话框
    TimePickerDialog(
        show = showTimePicker,
        description = "飞升报名时间为13:00-22:00",
        selectedTime = "13:00",
        minTime = "13:00",
        maxTime = "21:59",
        onTimeSelected = { timeString ->
            onTimeChange(timeString)
            showTimePicker = false
        },
        onDismiss = { showTimePicker = false }
    )
}

